package com.bnz.rabbit.demo1;

import com.rabbitmq.client.*;

/**
 * @author xy
 * @date 2022/10/26
 */
public class ConsumerClient {
    private final static String QUEUE_NAME = "hello";

    public static void main(String[] args)throws Exception {

        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("120.48.117.8");
        factory.setUsername("baizhi");
        factory.setPassword("123456!");
        Connection connection = factory.newConnection();
        Channel channel =connection.createChannel();
        System.out.println("等待接收消息");
        //推送 的消息进行接口回调
        DeliverCallback deliverCallback = (consumerTag,delivery)->
        {String message = new String(delivery.getBody());
            System.out.println(message);
        };
        CancelCallback cancelCallback=(consumerTag)->{
            System.out.println("消息消费被中断");
        };
        /**
         * 1.消费列队
         * 2.消费成功之后是否要自动应答
         * 3.消费者未成功消费回调
         */
        channel.basicConsume(QUEUE_NAME,true,deliverCallback,cancelCallback);
    }
}
